

*************************************************************************************************************
**
*Table A.6: Effects on market prices and overcharges
*This table reports estimated treatment effects on the market prices reported by beneffciaries, market prices reported by FPS dealers, statutory prices, and total overcharges (Panel D) in endline one (January - March)
*************************************************************************************************************



*******************************************************************************
* Household reported local market prices
*******************************************************************************
use "${SurveyDataDir}/JH_ePOS_HH_DataforAnalysis.dta",clear

keep if ss_code == "SS01"

drop if ghost_final==1

svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)


preserve
*Merge in original FPS codes                                              //merge in pweight to calculate fps_weight
use "${AdminDataDir}/FPS_sampling_prob.dta", clear 
rename pweight fps_weight 

keep district_code block_code fps_code fps_weight

collapse fps_weight, by(district_code block_code fps_code)



tempfile sampledat
save `sampledat', replace
restore


collapse(median) d2_co_*_kirana_price_fps d2_co_*_kirana_price_block pweight, by(district_code block_code fps_code strata treatment) 
merge m:1 district_code block_code fps_code using `sampledat', keep(3) nogen

rename fps_code old_fps_code
egen fps_code     = concat(district_code block_code old_fps_code)
encode fps_code, gen(fps_code_n)

merge m:1 fps_code using "C:\Users\tbrai\Dropbox\github_private\ABBA\replication\data\20220123_fps_spatial_exposure.dta", keep(3) nogen

*change varnames:
rename d2_co_1_kirana_price_fps d2_co_rice_kirana_price_fps
rename d2_co_2_kirana_price_fps d2_co_wheat_kirana_price_fps
rename d2_co_3_kirana_price_fps d2_co_sugar_kirana_price_fps
rename d2_co_4_kirana_price_fps d2_co_salt_kirana_price_fps
rename d2_co_5_kirana_price_fps d2_co_kero_kirana_price_fps

count
scalar fps_obs = 396

drop pweight
rename fps_weight pweight
svyset [pw = pweight]

loc ration "rice wheat sugar salt kero"

loc tflist " "
loc n = 0

foreach rat of local ration {

	loc n = `n' + 1

	foreach radius in 15 20 25 { 
	
		loc o = 2`n' + 1
	
		qui svy: mean d2_co_`rat'_kirana_price_fps if treatment == 0
		qui estat sd
		matrix Mean0 = r(mean)

		* Run regression
		xi: reg d2_co_`rat'_kirana_price_fps sp_exp_ex_`radius' treatment i.strata [pw = pweight], cluster(block_code)
		
		_estimates hold tempest`o', copy
		
		* Adjust estimates
		formatadjustedTEs `rat' `radius' tempest`o'
		
		eststo adj_`rat'_`radius'
		estadd scalar control_mean = `=Mean0[1,1]'
		estadd scalar percent_obs 100*e(N)/fps_obs

		tempfile mar_`rat'_`radius'
		parmest, label saving("mar_`rat'_`radius'", replace)
		local tflist "`tflist' mar_`rat'_`radius'"
		
	}
}


forval i = 2/4{
	dsconcat `tflist',subset(parm p in `i') dsn(ds_ration)       
	qqvalue p, method(yekutieli) qvalue(qval`i')
	list
	mkmat qval`i'
}
 
forval i = 1/5 {
	
	foreach radius in 15 20 25 {
	
		local estname: word `i' of `tflist'
		local rat: word `i' of `ration' 

		estimates restore adj_`rat'_`radius'  

		mat bet=e(b)                    

		mat bet[1,2]=qval2[`i',1]           
		mat bet[1,3]=qval3[`i',1]           
		mat bet[1,4]=qval4[`i',1]           

		mat list bet      

		estadd matrix qvalue = bet               
													
		mat list e(qvalue)   
	}
}

eststo dummy: reg p qval
estadd scalar control_mean=.,replace
estadd scalar N=.,replace	
	
loc tabname "${OutputDir}/TableA_6_adj.tex"
cd "${adoDir}"
loc nc 5

MultiPartTabStart, ///
			ncol(`nc') tabname(`tabname') ///
			colnames("Rice" "Wheat" "Sugar" "Salt" "Kerosene") width("\hsize") ///
			
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel A: Local market prices---reported by households")
			
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("adj_rice_20 adj_wheat_20 adj_sugar_20 adj_salt_20 adj_kero_20") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.64*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect ($0.64*\beta_N$)" )


eststo cr 





*******************************************************************************
* Dealer reported local market prices 
*******************************************************************************

use "${SurveyDataDir}/JHePOS_EL1_Dealer_data.dta",clear
tostring fps_uid, replace
drop if fps_uid == "."
merge 1:1 fps_uid using "${SurveyDataDir}/JHePOS_BL_Dealer_data.dta", keepusing(*_y0) nogen

tostring district_code, replace format("%02.0f")
tostring block_code, replace format("%03.0f")
tostring fps_code, replace format("%02.0f")


*Merge in Strata 
merge m:1 block_code using "${AdminDataDir}/blockLevel_strata.dta", keep(3) keepusing(strata)
drop _merge

qui distinct district_code block_code, j
assert `r(ndistinct)' == 132 

rename fps_code old_fps_code
egen fps_code     = concat(district_code block_code old_fps_code)
encode fps_code, gen(fps_code_n)

merge m:1 fps_code using "C:\Users\tbrai\Dropbox\github_private\ABBA\replication\data\20220123_fps_spatial_exposure.dta", keep(3) nogen

svyset [pw = pweight]


*Number of originally sampled FPS
scalar fps_obs = 396


local tflist_noBL ""
preserve
scalar obs = 396
keep if fps_new_yesno == 0

local ration "rice wheat sugar salt kero"

foreach rat of local ration{

	loc n = 0
	
	foreach radius in 15 20 25{

		loc o = 2`n' + 1

		qui svy: mean b14_cost_commodity_`rat' if treatment == 0 
		qui estat sd
		matrix Mean0 = r(mean)
		
		xi: reg b14_cost_commodity_`rat' sp_exp_ex_`radius' treatment i.strata [pw = pweight], cluster(block_code)
		_estimates hold tempest`o', copy

		* Adjust estimates
		formatadjustedTEs `rat' `radius' tempest`o'
		
		eststo mar_`rat'_`radius'
		estadd scalar control_mean = `=Mean0[1,1]'
		estadd scalar percent_obs 100*e(N)/obs

		tempfile mar_`rat'_`radius'
		parmest, label saving("mar_`rat'_`radius'", replace)
		local tflist_noBL "`tflist_noBL' mar_`rat'_`radius'"
			
	}

}

restore


forval i = 2/4{
	dsconcat `tflist_noBL',subset(parm p in `i') dsn(ds_ration)       
	qqvalue p, method(yekutieli) qvalue(qval`i')
	list
	mkmat qval`i'
}
 
forval i = 1/5 {
	
	foreach radius in 15 20 25 {
	
		local estname: word `i' of `tflist_noBL'
		local rat: word `i' of `ration' 

		estimates restore mar_`rat'_`radius'  

		mat bet=e(b)                    

		mat bet[1,2]=qval2[`i',1]           
		mat bet[1,3]=qval3[`i',1]           
		mat bet[1,4]=qval4[`i',1]           

		mat list bet      

		estadd matrix qvalue = bet               
													
		mat list e(qvalue)   
	}
}

eststo dummy: reg p qval
estadd scalar control_mean=.,replace
estadd scalar N=.,replace
	
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel B: Local market prices---reported by dealers (Rs)")
			
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("mar_rice_20 mar_wheat_20 mar_sugar_20 mar_salt_20 mar_kero_20") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.64*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect \tab($0.64*\beta_N$)" )

MultiPartTabEnd, ///
			ncol(`nc') tabname(`tabname') 
			
			
********************************************************************************
****** Alternative tables
loc tabname "${OutputDir}/TableA_6_p1_adj_sensitivity.tex"
cd "${adoDir}"
loc nc 5

MultiPartTabStart, ///
			ncol(`nc') tabname(`tabname') ///
			colnames("Rice" "Wheat" "Sugar" "Salt" "Kerosene") width("\hsize") ///
			
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("R = 15")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("adj_rice_15 adj_wheat_15 adj_sugar_15 adj_salt_15 adj_kero_15") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.61*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect \tab($0.61*\beta_N$)" )


MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring("R = 20")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("adj_rice_20 adj_wheat_20 adj_sugar_20 adj_salt_20 adj_kero_20") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.64*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect \tab($0.64*\beta_N$)" )
				
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring("R = 25")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("adj_rice_25 adj_wheat_25 adj_sugar_25 adj_salt_25 adj_kero_25") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.65*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect \tab($0.65*\beta_N$)" )

MultiPartTabEnd, ///
			ncol(`nc') tabname(`tabname') 
			
********************************************************************************
****** Alternative tables

loc tabname "${OutputDir}/TableA_6_p2_adj_sensitivity.tex"
cd "${adoDir}"
loc nc 5

MultiPartTabStart, ///
			ncol(`nc') tabname(`tabname') ///
			colnames("Rice" "Wheat" "Sugar" "Salt" "Kerosene") width("\hsize") ///
			
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("R = 15")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("mar_rice_15 mar_wheat_15 mar_sugar_15 mar_salt_15 mar_kero_15") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.61*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect \tab($0.61*\beta_N$)" )


MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring("R = 20")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("mar_rice_20 mar_wheat_20 mar_sugar_20 mar_salt_20 mar_kero_20") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.64*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect \tab($0.64*\beta_N$)" )
				
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring("R = 25")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("mar_rice_25 mar_wheat_25 mar_sugar_25 mar_salt_25 mar_kero_25") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_2 treatment _Istrata_3) ///
				order(_Istrata_2 treatment _Istrata_3) ///
				varlabels(	_Istrata_2 		"Adjusted TE ($\beta_{T}+0.65*\beta_{N}$)"  /// 
							treatment  		"Main effect ($\beta_T$)" ///  
							_Istrata_3		"Nbhd effect \tab($0.65*\beta_N$)" )

MultiPartTabEnd, ///
			ncol(`nc') tabname(`tabname') 

********************************************************************************
****** Alternative tables

loc tabname "${OutputDir}/TableA_6_adj_final.tex"
cd "${adoDir}"
loc nc 5

MultiPartTabStart, ///
			ncol(`nc') tabname(`tabname') ///
			colnames("Rice" "Wheat" "Sugar" "Salt" "Kerosene") width("\hsize") ///
			
MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel A: Local market prices---reported by households")
			
MultiPartTabPanelEndPlain, ///
				ncol(`nc') tabname(`tabname') ///
				models("adj_rice_15 adj_wheat_15 adj_sugar_15 adj_salt_15 adj_kero_15") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats() starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_3) 	///
				order(_Istrata_3) 	///
				varlabels(_Istrata_3 "$0.61*\beta_N (R = 15)$" )

MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring()
			
MultiPartTabPanelEndPlain, ///
				ncol(`nc') tabname(`tabname') ///
				models("adj_rice_20 adj_wheat_20 adj_sugar_20 adj_salt_20 adj_kero_20") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats() starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_3) ///
				order(_Istrata_3) ///
				varlabels(_Istrata_3		"$0.64*\beta_N (R = 20)$" )
				
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring()
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("adj_rice_25 adj_wheat_25 adj_sugar_25 adj_salt_25 adj_kero_25") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_3) ///
				order(_Istrata_3) ///
				varlabels(_Istrata_3		"$0.65*\beta_N (R = 25)$" )
				
MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel B: Local market prices---reported by dealers (Rs)")
			
MultiPartTabPanelEndPlain, ///
				ncol(`nc') tabname(`tabname') ///
				models("mar_rice_15 mar_wheat_15 mar_sugar_15 mar_salt_15 mar_kero_15") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats() starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_3) ///
				order(_Istrata_3) ///
				varlabels(_Istrata_3		"$0.61*\beta_N (R = 15)$" )

MultiPartTabPanelStartPlain, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring()
			
MultiPartTabPanelEndPlain, ///
				ncol(`nc') tabname(`tabname') ///
				models("mar_rice_20 mar_wheat_20 mar_sugar_20 mar_salt_20 mar_kero_20") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats() starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_3) ///
				order(_Istrata_3) ///
				varlabels(_Istrata_3		"$0.64*\beta_N (R = 20)$" )
				
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname')  ///
			panelstring()
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("mar_rice_25 mar_wheat_25 mar_sugar_25 mar_salt_25 mar_kero_25") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2))  )  ///
				stats(control_mean N, label("Control mean" "Observations") fmt(%12.2g %15.0fc)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				keep(_Istrata_3) ///
				order(_Istrata_3) ///
				varlabels(_Istrata_3		"$0.65*\beta_N (R = 25)$" )

MultiPartTabEnd, ///
			ncol(`nc') tabname(`tabname') 
